﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using ConstructionBidOnline.Biz;
using ConstructionBidOnline.Common;

public partial class ManageDisputeSubCategories : Page
{
    #region Types

    public enum LayoutType
    {
        Add,
        Edit,
        Search,
        View
    }

    #endregion

    #region Constants

    private const string PROP_DATASRC_DISPUTESUBCATEGORY = "__DISPUTESUBCATEGORY::DISPUTESUBCATEGORY";
    private const string PROP_DATASRC_DISPUTECATEGORY = "__DISPUTECATEGORY::DISPUTECATEGORY";
    private const string PROP_LAYOUT = "__PAGEVIDEO::UIL";
    private const string PROP_SELECTED_DISPUTESUBCATEGORY_ID = "__DISPUTESUBCATEGORY::SDISPUTESUBCATEGORYID";

    #endregion

    #region Public Properties

    public DisputeSubCategoryCollection DataSourceDisputeSubCategory
    {
        get { return (SessionMgr.Session[PROP_DATASRC_DISPUTESUBCATEGORY] is DisputeSubCategoryCollection ? (DisputeSubCategoryCollection)SessionMgr.Session[PROP_DATASRC_DISPUTESUBCATEGORY] : (DisputeSubCategoryCollection)(SessionMgr.Session[PROP_DATASRC_DISPUTESUBCATEGORY] = new DisputeSubCategoryCollection())); }
        set { SessionMgr.Session[PROP_DATASRC_DISPUTESUBCATEGORY] = value; }
    }

    public int SelectedDisputeSubCategoryID
    {
        get { return (SessionMgr.Session[PROP_SELECTED_DISPUTESUBCATEGORY_ID] is int ? (int)SessionMgr.Session[PROP_SELECTED_DISPUTESUBCATEGORY_ID] : 0); }
        set { SessionMgr.Session[PROP_SELECTED_DISPUTESUBCATEGORY_ID] = value; }
    }


    public DisputeCategoryCollection DataSourceDisputeCategory
    {
        get { return (SessionMgr.Session[PROP_DATASRC_DISPUTECATEGORY] is DisputeCategoryCollection ? (DisputeCategoryCollection)SessionMgr.Session[PROP_DATASRC_DISPUTECATEGORY] : (DisputeCategoryCollection)(SessionMgr.Session[PROP_DATASRC_DISPUTECATEGORY] = new DisputeCategoryCollection())); }
        set { SessionMgr.Session[PROP_DATASRC_DISPUTECATEGORY] = value; }
    }

    public LayoutType UILayout
    {
        get { return (SessionMgr.Session[PROP_LAYOUT] is LayoutType ? (LayoutType)SessionMgr.Session[PROP_LAYOUT] : LayoutType.Search); }
        set { SessionMgr.Session[PROP_LAYOUT] = value; }
    }




    #endregion

    #region Event Handlers

    #region Page Load

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Cache.SetCacheability(HttpCacheability.NoCache);

        if (!Page.IsPostBack)
        {
            DataSourceDisputeSubCategory = DisputeSubCategory.GetAllDisputeSubCategories();
            DisputeSubCategoryList1.DataBind(DataSourceDisputeSubCategory);
        }
    }

    #endregion

    #region Control: DisputeSubCategoryList

    protected void ucDisputeSubCategoryList_EditRequested(object sender, Controls_DisputeSubCategoryList.EditRequestedEventArgs evt)
    {
        SelectedDisputeSubCategoryID = evt.DSC.SubCategoryId;
        DisputeSubCategoryViewEdit1.DataBind(evt.DSC, DataSourceDisputeCategory);
        SetLayout(LayoutType.Edit);
        divexistname.Visible = false;

    }

    protected void ucDisputeSubCategoryList_AddNewRequested(object sender, EventArgs evt)
    {
        DisputeSubCategoryViewEdit1.CancelEdit();
        DisputeSubCategory newDisputeSubCategory = DisputeSubCategory.CreateNew(DataSourceDisputeSubCategory);
        SelectedDisputeSubCategoryID = newDisputeSubCategory.SubCategoryId;
        DataSourceDisputeCategory = DisputeCategory.GetAllDisputeCategories();
        DisputeSubCategoryViewEdit1.DataBind(DataSourceDisputeSubCategory.FindByDisputeSubCategoryID(SelectedDisputeSubCategoryID), DataSourceDisputeCategory);
        SetLayout(LayoutType.Edit);
        divexistname.Visible = false;

    }


    protected void ucDisputeSubCategoryList_DataSourceRequested(object sender, EventArgs evt)
    {
        DataSourceDisputeSubCategory = DisputeSubCategory.GetAllDisputeSubCategories();
        DisputeSubCategoryList1.DataSource = DataSourceDisputeSubCategory;
    }

    #endregion

    #region Control: DisputeSubCategoryViewEdit

    protected void ucDisputeSubCategoryViewEdit_DataSourceUserRequested(object sender, EventArgs evt)
    {
        DisputeSubCategoryViewEdit1.DataSourceDisputeSubCategory = DataSourceDisputeSubCategory.FindByDisputeSubCategoryID(SelectedDisputeSubCategoryID);
    }

    #endregion

    #region Save / Cancel Buttons

    protected void btnCancel_Click(object sender, ImageClickEventArgs e)
    {
        DisputeSubCategoryViewEdit1.CancelEdit();
        // If we were adding a new video...then remove the incomplete/not added new jobsubcategory object from the collection.
        if (DataSourceDisputeSubCategory.FindByDisputeSubCategoryID(SelectedDisputeSubCategoryID).ObjState == BOBase.ObjectState.Added)
        {
            try
            {
                DataSourceDisputeSubCategory.Remove(DataSourceDisputeSubCategory.FindByDisputeSubCategoryID(SelectedDisputeSubCategoryID));
                SelectedDisputeSubCategoryID = (DataSourceDisputeSubCategory[0] != null ? DataSourceDisputeSubCategory[0].SubCategoryId : 0);
            }
            catch
            {
            }
        }
        DisputeSubCategoryList1.DataBind(DataSourceDisputeSubCategory);
        SetLayout(LayoutType.View);
    }

    protected void btnsave_Click(object sender, ImageClickEventArgs e)
    {
        if (DisputeSubCategoryViewEdit1.EndEdit())
        {
            DisputeSubCategory obj = DataSourceDisputeSubCategory.FindByDisputeSubCategoryID(SelectedDisputeSubCategoryID);
            DisputeSubCategory.Update(obj);

            // TODO: Set Feedback text...

            DisputeSubCategoryList1.DataBind(DataSourceDisputeSubCategory);
            SetLayout(LayoutType.View);
            divexistname.Visible = false;

        }
        else
        {
            divexistname.Visible = true;
        }
    }


    #endregion

    #endregion

    #region Private

    #region SetLayout

    /// <summary>
    /// Configures the UI for the specified 'uiLayoutType'.
    /// </summary>
    protected void SetLayout(LayoutType uiLayoutType)
    {
        switch (uiLayoutType)
        {
            case LayoutType.Add:
            case LayoutType.Edit:

                pnlDisputeSubCategoryList.Visible = false;
                pnlDisputeSubCategoryViewEdit.Visible = true;
                pnlButtons.Visible = true;
                break;

            case LayoutType.View:

            case LayoutType.Search:

                pnlDisputeSubCategoryList.Visible = true;
                pnlDisputeSubCategoryViewEdit.Visible = false;
                pnlButtons.Visible = false;
                break;

            default:
                throw new NotImplementedException("The specified LayoutType is not supported in this release.");
        }

        //SetInitialFocus(uiLayoutType);
    }

    #endregion

    #endregion


}
